package com.oss.model;

import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.NotPersistent;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Text;
import com.google.appengine.api.users.User;

/**
 * @author xueqiang.mi
 * @date 2010-2-2
 */
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Project implements Serializable {
	private static final long serialVersionUID = 5860162877435752783L;

	@PrimaryKey
	private String id;
	@Persistent
	private String name;
	@Persistent
	private String source;
	@Persistent
	private String url;
	@Persistent
	private String license;
	@Persistent(serialized = "true", defaultFetchGroup = "true")
	private Text description;
	@Persistent
	private String label = "notSet"; // 自定义标签
	@Persistent
	private User createdAuthor;
	@Persistent
	private Date createdDate = new Date();
	@Persistent
	private String tag = "notSet"; // 标准标签
	@Persistent
	private Set<Version> versions = null;
	@Persistent
	private Boolean enabled = true;
	@Persistent
	private Boolean frozen = false;
	@Persistent
	private Long rank = 65535L;
	@Persistent
	private Long linkQuantity = 0L;
	@NotPersistent
	private String str_desc;

	public Project() {
	}

	public Project(String name, String source, String url) {
		this(name, null, source, url);
	}

	public Project(String name, String license, String source, String url) {
		this.name = name;
		this.source = source;
		this.url = url;

		// 将来源与项目名称加入自定义标签中
		StringBuilder sb = new StringBuilder();
		sb.append(source.toLowerCase()).append(" ").append(name.toLowerCase());
		this.label = sb.toString();

		// 设置项目的授权协议
		if (license != null) {
			this.license = license;
		} else {
			this.license = "notSet";
		}
	}

	public void add1Link() {
		this.linkQuantity++;
	}

	public void addVersion(Version version) {
		if (getVersions() == null) {
			this.versions = new HashSet<Version>();
		}
		getVersions().add(version);
	}

	public void delete1Link() {
		this.linkQuantity--;
	}

	public User getCreatedAuthor() {
		return createdAuthor;
	}

	public Date getCreatedDate() {
		return createdDate;
	}

	public String getDescription() {
		if (description != null) {
			return description.getValue();
		}
		return null;
	}

	public Boolean getEnabled() {
		return enabled;
	}

	public Boolean getFrozen() {
		return frozen;
	}

	public String getId() {
		return id;
	}

	public String getLabel() {
		return label;
	}

	public String getLicense() {
		return license;
	}

	public Long getLinkQuantity() {
		return linkQuantity;
	}

	public String getName() {
		return name;
	}

	public Long getRank() {
		return rank;
	}

	public String getSource() {
		return source;
	}

	public String getStr_desc() {
		return str_desc;
	}

	public String getTag() {
		return tag;
	}

	public String getUrl() {
		return url;
	}

	public Set<Version> getVersions() {
		return versions;
	}

	public boolean isNew() {
		return (this.id == null);
	}

	public void setCreatedAuthor(User createdAuthor) {
		this.createdAuthor = createdAuthor;
	}

	public void setCreatedDate(Date createdDate) {
		this.createdDate = createdDate;
	}

	public void setDescription(String description) {
		this.description = new Text(description);
	}

	public void setEnabled(Boolean enabled) {
		this.enabled = enabled;
	}

	public void setFrozen(Boolean frozen) {
		this.frozen = frozen;
	}

	public void setId(String id) {
		this.id = id;
	}

	public void setLabel(String label) {
		this.label = label;
	}

	public void setLicense(String license) {
		this.license = license;
	}

	public void setLinkQuantity(Long linkQuantity) {
		this.linkQuantity = linkQuantity;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setRank(Long rank) {
		this.rank = rank;
	}

	public void setSource(String source) {
		this.source = source;
	}

	public void setStr_desc(String strDesc) {
		str_desc = strDesc;
	}

	public void setTag(String tag) {
		this.tag = tag;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public void setVersions(Set<Version> versions) {
		this.versions = versions;
	}

	@Override
	public String toString() {
		return "Project [id=" + id + ", name=" + name + ",description="
				+ description + ",  url=" + url + "]";
	}

}